﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace EnerForecast
{
    class ForeCastResult
    {
        private int month,day;
        private double mintemp;
        private double[] coe_temp_cost,coe_temp_cost_heat;
        private double[] coe_cost_hour17, coe_cost_hour8, coe_cost_hour17_heat, coe_cost_hour8_heat;
        public  ForeCastResult(DateTime date,double MinTemp,double MaxTemp)
        {
            month = date.Month;
            day = date.Day;
            mintemp = MinTemp;  
        }
        //获取平滑后曲线值
        public double[] getSmoothedResult(double[] X,double[] Y,double a=0.3)
        {
            double[] Z = new double[X.GetLength(0)];
            double[] S1 = new double[X.GetLength(0)];
            double[] S2 = new double[X.GetLength(0)];
            S1[0] = X[0];
            S2[0] = X[0];
            try
            {

                S1[1] = X[1] + a * (Y[0] - X[0]);
                S2[1] = S1[1];
                for (int i = 2; i < X.GetLength(0); i++)
                {
                    //一阶nomal
                    S1[i] = X[i] + (1 - a) * (Y[i - 1] - X[i - 1]);
                    ////一阶ss
                    //S1[i] = Y[i - 1] + (X[i] - X[i - 1]) + a * (Y[i - 1] - X[i - 1]);

                    ////一阶s
                    //S1[i] = a * X[i] + (1 - a) * S1[i - 1];
                    ////二阶
                    //S2[i] = a * S1[i] + (1 - a) * S2[i - 1];
                }
                //for (int i = 0; i < X.GetLength(0); i++)
                //{
                //    Z[i] = (2 - a) / (1 - a) * S1[i] - S2[i] / (1 - a);
                //}
            }
            catch (Exception e)
            {
            }
            //二次指数移动法
            //Z[0] = X[0];
            //for (int i = 1; i < 8; i++)
            //{
            //    Z[i] = X[i] + a * (Y[i - 1] - X[i - 1]);
            //}
            //for (int i = 8; i < X.GetLength(0); i++)
            //{
            //    S1[i]=0;
            //    for (int j = 7; j < i; j++)
            //    {
            //        S1[i] += Y[j];
            //    }
            //    S1[i] = S1[i] / (i - 7);
            //    S2[i] = 0;
            //    for (int j = 7; j < i+1; j++) {
            //        S2[i] += S1[j];
            //    }
            //    S2[i] = S2[i] / (i - 7);
            //    Z[i] = 2 * S1[i] - S2[i] + 2 / (i - 7) * (S1[i] - S2[i]);
            //}
            return S1;
        }
        //获取历史数据某月某时刻测试用
        public double[] getPostCost()
        {
            double[] postCost = new double[24];
            ForeCastDataSet.tb_costPhourDataTable dt = new ForeCastDataSet.tb_costPhourDataTable();
            ForeCastDataSetTableAdapters.tb_costPhourTableAdapter tadapter = new ForeCastDataSetTableAdapters.tb_costPhourTableAdapter();
            for (int i = 0; i < 24; i++)
            {
                try
                {
                    dt = tadapter.GetSpecificData(month, i + 1);
                    DataRow[] dr = dt.Select("Time like '" + month.ToString() + "/" + day.ToString() + "%' ");
                    postCost[i] = (double)dr[0]["Cost"];
                }
                catch (Exception)
                {
                    postCost[i] = 0;
                }
            }
            return postCost;
        }
        //获取预测数据
        public double[] getComputeResult()
        {
            //计算

            DataTable tb_result = new DataTable("resulttable");     
            double[] computeResult = new double[24];
            for (int i = 0; i < 7; i++)
            {
                computeResult[i] = compute(coe_cost_hour17[0], coe_cost_hour17[1], coe_cost_hour17[2], coe_cost_hour17[3], i + 1);
            }
            for (int i = 7; i < 24; i++)
            {
                
       
                computeResult[i] = compute(coe_cost_hour8[0], coe_cost_hour8[1], coe_cost_hour8[2], coe_cost_hour8[3], i + 1);
            }
            double total = compute(coe_temp_cost[0], coe_temp_cost[1], coe_temp_cost[2], coe_temp_cost[3], mintemp);
            //加法公式，平移
            for (int i = 0; i < 24; i++)
            {
                total = total - computeResult[i];
            }
            //total = total / (24 - 7);
            //for (int i = 7; i < 24; i++)
            //{
            //    computeResult[i] = computeResult[i] + total;
            //}
            //tb_result.Columns.Add("时刻", System.Type.GetType("System.Int32"));
            //tb_result.Columns.Add("能耗", System.Type.GetType("System.String"));
            ////存入table
            //for (int i = 0; i < 24; i++)
            //{
            //    DataRow dr = tb_result.NewRow();
            //    dr["时刻"] = i + 1;
            //    dr["能耗"] = computeResult[i].ToString("0.00");
            //    tb_result.Rows.Add(dr);
            //}
            return computeResult;
        } 
        private double compute(double a,double b,double c,double d,double x){
            return a * x * x * x + b * x * x + c * x + d;
        }
        public  bool checkCoe()
        {
            //冷
            ForeCastDataSetTableAdapters.tb_coefficientTableAdapter temp_coe = new ForeCastDataSetTableAdapters.tb_coefficientTableAdapter();
            ForeCastDataSet.tb_coefficientDataTable temp_coe_tb = new ForeCastDataSet.tb_coefficientDataTable(); //冷
            //热
            ForeCastDataSetTableAdapters.tb_coefficient1TableAdapter temp_coe_heat = new ForeCastDataSetTableAdapters.tb_coefficient1TableAdapter();
            ForeCastDataSet.tb_coefficient1DataTable temp_coe_tb_heat = new ForeCastDataSet.tb_coefficient1DataTable(); //热
            temp_coe_tb = temp_coe.GetDataByID(0);
            temp_coe_tb_heat = temp_coe_heat.GetDataByID(0);
            if (temp_coe_tb.Rows.Count != 1&&temp_coe_tb_heat.Rows.Count!=1)
            {
                return false;
            }
            else
            {
               //leng
                coe_temp_cost = new double[4];
                coe_temp_cost[0] = (double)temp_coe_tb.Rows[0]["a"];
                coe_temp_cost[1] = (double)temp_coe_tb.Rows[0]["b"];
                coe_temp_cost[2] = (double)temp_coe_tb.Rows[0]["c"];
                coe_temp_cost[3] = (double)temp_coe_tb.Rows[0]["d"]; //leng
                //re
                coe_temp_cost_heat = new double[4]; 
                coe_temp_cost_heat[0] = (double)temp_coe_tb_heat.Rows[0]["a"];
                coe_temp_cost_heat[1] = (double)temp_coe_tb_heat.Rows[0]["b"];
                coe_temp_cost_heat[2] = (double)temp_coe_tb_heat.Rows[0]["c"];
                coe_temp_cost_heat[3] = (double)temp_coe_tb_heat.Rows[0]["d"]; //re
            }
            //1~7
            temp_coe_tb = temp_coe.GetDataByID(2*month-1); //leng
            temp_coe_tb_heat=temp_coe_heat.GetDataByID(2*month-1); //re
            if (temp_coe_tb.Rows.Count != 1 && temp_coe_tb_heat.Rows.Count !=1)
            {
                return false;
            }
            else
            {
                //leng
                coe_cost_hour17 = new double[4];
                coe_cost_hour17[0] = (double)temp_coe_tb.Rows[0]["a"];
                coe_cost_hour17[1] = (double)temp_coe_tb.Rows[0]["b"];
                coe_cost_hour17[2] = (double)temp_coe_tb.Rows[0]["c"];
                coe_cost_hour17[3] = (double)temp_coe_tb.Rows[0]["d"]; //leng
                //re
                 coe_cost_hour17_heat = new double[4];
                coe_cost_hour17_heat[0] = (double)temp_coe_tb_heat.Rows[0]["a"];
                coe_cost_hour17_heat[1] = (double)temp_coe_tb_heat.Rows[0]["b"];
                coe_cost_hour17_heat[2] = (double)temp_coe_tb_heat.Rows[0]["c"];
                coe_cost_hour17_heat[3] = (double)temp_coe_tb_heat.Rows[0]["d"]; //re
                
            }
            //8~24
            temp_coe_tb = temp_coe.GetDataByID(2*month);//leng
            temp_coe_tb_heat=temp_coe_heat.GetDataByID(2*month);//re
            if (temp_coe_tb.Rows.Count != 1 && temp_coe_tb_heat.Rows.Count !=1)
            {
                return false;
            }
            else
            {
                //leng
                coe_cost_hour8 = new double[4];
                coe_cost_hour8[0] = (double)temp_coe_tb.Rows[0]["a"];
                coe_cost_hour8[1] = (double)temp_coe_tb.Rows[0]["b"];
                coe_cost_hour8[2] = (double)temp_coe_tb.Rows[0]["c"];
                coe_cost_hour8[3] = (double)temp_coe_tb.Rows[0]["d"]; //leng
                //re
                coe_cost_hour8_heat = new double[4];
                coe_cost_hour8_heat[0] = (double)temp_coe_tb_heat.Rows[0]["a"];
                coe_cost_hour8_heat[1] = (double)temp_coe_tb_heat.Rows[0]["b"];
                coe_cost_hour8_heat[2] = (double)temp_coe_tb_heat.Rows[0]["c"];
                coe_cost_hour8_heat[3] = (double)temp_coe_tb_heat.Rows[0]["d"]; //re
            }
            return true;
        }
        public double[] getPostCost1()    //热负荷
        {
            double[] postCost = new double[24];
            ForeCastDataSet.tb_costPhour1DataTable dt = new ForeCastDataSet.tb_costPhour1DataTable();
            ForeCastDataSetTableAdapters.tb_costPhour1TableAdapter tadapter = new ForeCastDataSetTableAdapters.tb_costPhour1TableAdapter();
            for (int i = 0; i < 24; i++)
            {
                try
                {
                    dt = tadapter.GetSpecificData(month, i + 1);
                    DataRow[] dr = dt.Select("Time like '" + month.ToString() + "/" + day.ToString() + "%' ");
                    postCost[i] = (double)dr[0]["Cost"];
                }
                catch (Exception)
                {
                    postCost[i] = 0;
                }
            }
            return postCost;
        }
        public double[] getComputeResultHeat()
        {
            //计算

            DataTable tb_result = new DataTable("resulttable");
            double[] computeResult = new double[24];
            for (int i = 0; i < 7; i++)
            {
                computeResult[i] = compute(coe_cost_hour17_heat[0], coe_cost_hour17_heat[1], coe_cost_hour17_heat[2], coe_cost_hour17_heat[3], i + 1);
            }
            for (int i = 7; i < 24; i++)
            {


                computeResult[i] = compute(coe_cost_hour8_heat[0], coe_cost_hour8_heat[1], coe_cost_hour8_heat[2], coe_cost_hour8_heat[3], i + 1);
            }
            double total = compute(coe_temp_cost_heat[0], coe_temp_cost_heat[1], coe_temp_cost_heat[2], coe_temp_cost_heat[3], mintemp);
            //加法公式，平移
            for (int i = 0; i < 24; i++)
            {
                total = total - computeResult[i];
            }
            //total = total / (24 - 7);
            //for (int i = 7; i < 24; i++)
            //{
            //    computeResult[i] = computeResult[i] + total;
            //}
            //tb_result.Columns.Add("时刻", System.Type.GetType("System.Int32"));
            //tb_result.Columns.Add("能耗", System.Type.GetType("System.String"));
            ////存入table
            //for (int i = 0; i < 24; i++)
            //{
            //    DataRow dr = tb_result.NewRow();
            //    dr["时刻"] = i + 1;
            //    dr["能耗"] = computeResult[i].ToString("0.00");
            //    tb_result.Rows.Add(dr);
            //}
            return computeResult;
        } 
    }
}
